package com.xique.park.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.domain.R;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.bean.domain.ParkInfo;
import com.xique.park.bean.domain.ParkOn;
import com.xique.park.bean.domain.ParkXjReduction;
import com.xique.park.mapper.ParkInfoMapper;
import com.xique.park.mapper.ParkOnMapper;
import com.xique.park.mapper.ParkXjReductionMapper;
import com.xique.park.service.service.IParkXjReductionService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Log4j2
public class ParkXjReductionServiceImpl extends ServiceImpl<ParkXjReductionMapper, ParkXjReduction>
        implements IParkXjReductionService {

    @Autowired
    private ParkInfoMapper parkInfoMapper;

    @Autowired
    private ParkOnMapper parkOnMapper;

    @Override
    public AjaxResult getReduction(ParkXjReduction parkXjReduction, String parkUuid) {

        QueryWrapper<ParkInfo> queryWrapp = new QueryWrapper<ParkInfo>();
        queryWrapp.eq("park_uuid", parkUuid);
        ParkInfo parkInfo = parkInfoMapper.selectOne(queryWrapp);
        if (parkInfo == null) {
            log.info("小桔没有对应场站");
            return AjaxResult.error(10006, "没有对应场站", null);
        }
        parkXjReduction.setParkId(parkInfo.getId());
        QueryWrapper<ParkOn> query = new QueryWrapper<ParkOn>();
        query.eq("park_id", parkXjReduction.getParkId());
        query.eq("plate", parkXjReduction.getPlate());
        ParkOn parkOn = parkOnMapper.selectOne(query);
        log.info("[小桔充电]查询在场车辆信息,参数park_id：{},plate：{},结果:{}", parkXjReduction.getParkId(), parkXjReduction.getPlate(), parkOn);
        if (parkOn == null) {
            log.info("小桔车辆未入场");
//            return AjaxResult.error(10002, "车辆未入场", null);
        }
        QueryWrapper<ParkXjReduction> queryWrapper = new QueryWrapper<ParkXjReduction>();
        queryWrapper.eq("plate", parkXjReduction.getPlate());
        queryWrapper.eq("park_id", parkXjReduction.getParkId());
        queryWrapper.eq("relief_status", 0);
        Map<String, Date> map = new HashMap<>();
        map.put("carEntryTime", null == parkOn ? parkXjReduction.getStartChargingTime() : parkOn.getInTime());
        ParkXjReduction parkXjReduction1 = baseMapper.selectOne(queryWrapper);
        if (parkXjReduction1 != null) {
            log.info("重复减免");
            return AjaxResult.error(10003, "重复减免", map);
        }
        int i = baseMapper.insert(parkXjReduction);
        if (i > 0) {
            return AjaxResult.success(10000, "减免成功", map);
        }
        log.info("停车场错误");
        return AjaxResult.error(10005, "停车场错误", null);
    }

    @Override
    public ParkXjReduction getPlate(String plate, Integer plateId) {
        QueryWrapper queryWrapper = new QueryWrapper<ParkXjReduction>();
        queryWrapper.eq("plate", plate);
        queryWrapper.eq("park_id", plateId);
        queryWrapper.eq("relief_status", 0);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public R updateStatus(ParkXjReduction parkXjReduction) {
        QueryWrapper queryWrapper = new QueryWrapper<ParkXjReduction>();
        queryWrapper.eq("plate", parkXjReduction.getPlate());
        queryWrapper.eq("park_id", parkXjReduction.getParkId());
        queryWrapper.eq("relief_status", 0);
        parkXjReduction = baseMapper.selectOne(queryWrapper);
        if (parkXjReduction != null) {
            parkXjReduction.setReliefStatus("1");
            parkXjReduction.setUpdateTime(new Date());
            int i = baseMapper.updateById(parkXjReduction);
            if (i > 0) {
                log.info("减免状态修改成功");
                return R.ok(null, "减免状态修改成功");
            } else {
                log.info("减免状态修改失败");
                return R.ok(null, "减免状态修改失败");
            }
        }
        return R.fail(null, "没有该车辆的待减免状态修改");
    }
}
